package jaux.tank.bean.entity.system;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import jaux.tank.bean.core.ShiroUser;
import jaux.tank.shiro.ShiroKit;
import lombok.Getter;
import lombok.Setter;
@MappedSuperclass
@Getter
@Setter
public class EntityWithAudit extends EntityWithObjectId{
	public static final String DATETIME_FORMAT= "yyyy-MM-dd HH:mm:ss";
	
	@PrePersist
	protected void prePersist() {
		createTime = DateTimeFormatter.ofPattern(DATETIME_FORMAT).format(LocalDateTime.now());
		updateTime = createTime;
		Optional<ShiroUser> user = ShiroKit.getUser();
		if(user.isPresent()) {
			createdBy = user.get().getId();
			updatedBy = createdBy;
		}
		version = 1;
	}
	@PreUpdate
	protected void preUpdate() {
		updateTime = DateTimeFormatter.ofPattern(DATETIME_FORMAT).format(LocalDateTime.now());
		Optional<ShiroUser> user = ShiroKit.getUser();
		if(user.isPresent()) {
			updatedBy = user.get().getId();
		}
		version = this.version+1;
	}

    private String createTime;

    private String updateTime;
    
    private String createdBy;
    
    private String updatedBy;
    
    private int version;

}
